﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using PearTunes.Model;
using PearTunes.DAL;
using System.IO;
using PearTunes.WEB.Models;

namespace PearTunes.WEB
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "PearTunesService" in code, svc and config file together.
    /// <summary>
    /// Class for standard PearTunes services
    /// </summary>
    public class PearTunesService : IPearTunesService
    {
        /// <summary>
        /// Gets the collection of albums
        /// </summary>
        /// <returns>Collection of albums</returns>
        public ICollection<Model.Album> GetAlbums()
        {
            var context = new PearTunesEntities();
            return context.Albums.Include("Tracks").ToList();
        }

        /// <summary>
        /// Search for an album by album title
        /// </summary>
        /// <param name="albumtitle">Album title to search for</param>
        /// <returns>The first album match</returns>
        public ICollection<Album> SearchAlbum(string albumtitle)
        {
            return (new PearTunesEntities()).Albums.Where(a => a.Title==albumtitle).ToList();
        }
        /// <summary>
        /// Gets the collection of tracks
        /// </summary>
        /// <returns>Collection of tracks</returns>
        public ICollection<Track> GetTracks()
        {
            return (new PearTunesEntities()).Tracks.ToList();
        }
        /// <summary>
        /// Search for a track by track title
        /// </summary>
        /// <param name="tracktitle">Track title to search for</param>
        /// <returns>Collection of matching tracks</returns>
        public ICollection<Track> SearchTrack(string tracktitle)
        {
            return (new PearTunesEntities()).Tracks.Where(t => t.Title==tracktitle).ToList();
        }
        /// <summary>
        /// Gets the info of an user by user name
        /// </summary>
        /// <param name="username">User name</param>
        /// <returns>User object with matching user name</returns>
        public User GetUserInfo(string username)
        {
            return (new PearTunesEntities()).Users.Where(u => u.Name==username).First();
        }
        /// <summary>
        /// Gets a user's bought tracks list
        /// </summary>
        /// <param name="username">User name</param>
        /// <returns>The collection of track that the user has bought</returns>
        public ICollection<Track> GetUserTracks(string username) //lehet hogy felesleges!?
        {
            return GetUserInfo(username).BoughtTracks.ToList();
        }
        /// <summary>
        /// Gets a file stream by path
        /// </summary>
        /// <param name="path">Path of the file to stream</param>
        /// <returns>The stream of the given track</returns>
        public Stream GetFileStream(string path)
        {
            return new FileStream(path, FileMode.Open);
            
        }


        /// <summary>
        /// The user buyes a track
        /// </summary>
        /// <param name="username">The user's name who has bought the track</param>
        /// <param name="t">The track entity that has been bought</param>
        public void BuyTrack(string username, Track t)
        {
            var context = new PearTunesEntities();
            User buyer = context.Users.Include("BoughtTracks").Where(u => u.Name == username).First();
            if (!buyer.BoughtTracks.Contains(context.Tracks.Where(tra => tra.TrackId == t.TrackId).First()))
            {
                buyer.Money -= t.Price;
                buyer.BoughtTracks.Add(context.Tracks.Where(tra => tra.TrackId == t.TrackId).First());
            }
            context.SaveChanges();
        }

        /// <summary>
        /// Valides the user by user name and password
        /// </summary>
        /// <param name="username">User name</param>
        /// <param name="pass">Password of the user</param>
        /// <returns>True if the username and password matches, false in other cases</returns>
        public bool ValidateUser(string username, string pass)
        {
             return (new AccountMembershipService()).ValidateUser(username, pass);
        }
    }
}
